package org.csu.jpetmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Null;
import org.csu.jpetmanage.mapper.CategoryMapper;
import org.csu.jpetmanage.model.Admin;
import org.csu.jpetmanage.model.Category;
import org.csu.jpetmanage.model.Order;
import org.csu.jpetmanage.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping
public class CategoryController {

    //实现数据的主键校验，也就是判断逐渐是否存在相同的情况，存在就要提醒用户更改id值
    @Autowired
    CategoryService categoryService;

    @Autowired
    Category category;

    @Autowired
    CategoryMapper categoryMapper;

    //新填一个string变量用于储存当前的操作的信息以及结果，并且用model传到前端页面categoryManage上面
    String message = null;

    @GetMapping("/ManageQueryAll")
//    @RequestBody()
    public String queryCategory( Model model){
        List list =categoryService.list();
        model.addAttribute("visitor_list" ,list);
        return "categoryManage";
    }

    //实现数据的增添操作

    @GetMapping(path = "/MangeInsert")
    public String ManageInsert( @RequestParam("catid") String catid, @RequestParam("name1") String name, @RequestParam("descn") String descn,  Model model,HttpSession session){
        category.setCatid(catid);
        category.setName(name);
        category.setDescn(descn);
        int a = categoryMapper.insert(category);
        if (a == 1){
            message ="新增成功！！请继续使用";
            model.addAttribute("feedback", message);
            session.setAttribute("msg",message);
        }
        return "redirect:/ManageQueryAll";
    }

    //实现数据库的删除操作
    @GetMapping ("/ManageDelete")
    public String ManageDelete( @RequestParam("catid") String catid, @RequestParam("name1") String name, @RequestParam("descn") String descn, Model model , HttpSession session){
        category.setCatid(catid);
        category.setName(name);
        category.setDescn(descn);
        int a = categoryMapper.deleteById(category);
        if (a == 1){
            message ="删除成功！！请继续使用";
            model.addAttribute("feedback", message);
            session.setAttribute("msg",message);
        }
        return "redirect:/ManageQueryAll";
    }

    //实现数据库的修改操作

    @GetMapping ("/ManageUpdate")
    public String ManageUpdate(@RequestParam("catid") String catid,@RequestParam("name1") String name,@RequestParam("descn") String descn, Model model, HttpSession session){
        category.setCatid(catid);
        category.setName(name);
        category.setDescn(descn);
        int a = categoryMapper.updateById(category);
        if (a == 1){
            message ="修改成功！！请继续使用";
            model.addAttribute("feedback", message);
            session.setAttribute("msg",message);
        }
        return "redirect:/ManageQueryAll";
    }


    @GetMapping("/CategoryEditAjax")
    @ResponseBody
    public Map<String,Object> CategoryEditAjax(
            @RequestParam("catid1") String catid,
            HttpSession session
    ){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("catid",catid);//是根据Category实体类中的属性来进行取值
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        if (categoryList.size()==1){
            System.out.println("CategoryList size is 1");
            category = categoryList.get(0);
            session.setAttribute("editOrder",category);//待会处理编辑数据的时候需要用
            //把下面补全
            map.put("catid",category.getCatid());
            map.put("name1",category.getName());
            map.put("descn",category.getDescn());
            return map;
        }
        return map;
    }


    @RequestMapping("/searchCategory")
    public String searchItem(
            @RequestParam("keyword") String catid,
            Model model){
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        System.out.println(catid);
        queryWrapper.like("catid",catid).
                or().like("name",catid).
                or().like("descn",catid);
        List<Category> categoryList = categoryMapper.selectList(queryWrapper);
        model.addAttribute("visitor_list" ,categoryList);
        return "categoryManage";
    }


    @GetMapping("/categorySearchAjax")
    @ResponseBody
    public List<Category> categorySearchAjax(
            @RequestParam("keyword") String keyword,
            HttpSession session){
        System.out.println(keyword+" in CategoryController");
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("catid",keyword).
                or().like("name",keyword).
                or().like("descn",keyword);
        return categoryMapper.selectList(queryWrapper);
    }

}




